package com.example.mvvm.until;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;

import com.example.mvvm.R;


/**
 * @author: limuyang
 * @date: 2019-11-29
 * @Description:
 */
public class Tips {
    public static int FINISH = 1;
    public static int ERROR = 2;
    public static int WARNING = 3;
    public final static int ICON_FINISH = R.drawable.finish_ic;
    public final static int ICON_ERROR = R.drawable.error_ic;
    public final static int ICON_WARNING = R.drawable.warning_ic;

    /**
     * 显示 Toast
     *
     * @param message 提示信息
     */
    public static void show(Context context, String message) {
        show(context, message, Toast.LENGTH_SHORT);
    }

    public static void show(Context context, @StringRes int message) {
        show(context, context.getString(message));
    }

    /**
     * 显示 Toast
     *
     * @param message  提示信息
     * @param duration 显示时间长短
     */
    public static void show(Context context, String message, int duration) {
        Toast toast = new Toast(context);
        toast.setDuration(duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(createTextToastView(context, message));
        toast.show();
    }

    public static void showStatus(Context context, int duration, int status, String message) {
        Toast toast = new Toast(context);
        toast.setDuration(duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(createStatusToastView(context, message, status));
        toast.show();
    }

    /**
     * 创建自定义 Toast View
     *
     * @param message 文本消息
     * @return View
     */
    private static View createTextToastView(Context context, String message) {
        // 画圆角矩形背景
        float rc = dp2px(context, 6);
        RoundRectShape shape = new RoundRectShape(new float[]{rc, rc, rc, rc, rc, rc, rc, rc}, null, null);
        ShapeDrawable drawable = new ShapeDrawable(shape);
        drawable.getPaint().setColor(Color.argb(225, 240, 240, 240));
        drawable.getPaint().setStyle(Paint.Style.FILL);
        drawable.getPaint().setAntiAlias(true);
        drawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);

        // 创建View
        FrameLayout layout = new FrameLayout(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(layoutParams);
        layout.setPadding(dp2px(context, 16), dp2px(context, 12), dp2px(context, 16), dp2px(context, 12));
        layout.setBackground(drawable);

        TextView textView = new TextView(context);
        textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        textView.setTextSize(15);
        textView.setText(message);
        textView.setLineSpacing(dp2px(context, 4), 1f);
        textView.setTextColor(Color.BLACK);

        layout.addView(textView);

        return layout;
    }


    /**
     * 创建自定义 Toast View
     *
     * @param message 文本消息
     * @return View
     */
    private static View createStatusToastView(Context context, String message, int status) {
        // 画圆角矩形背景
        float rc = dp2px(context, 6);
        RoundRectShape shape = new RoundRectShape(new float[]{rc, rc, rc, rc, rc, rc, rc, rc}, null, null);
        ShapeDrawable drawable = new ShapeDrawable(shape);
        drawable.getPaint().setColor(Color.parseColor("#D9000000"));
        drawable.getPaint().setStyle(Paint.Style.FILL);
        drawable.getPaint().setAntiAlias(true);
        drawable.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);

        // 创建View
        LinearLayout layout = new LinearLayout(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(layoutParams);
        layout.setGravity(Gravity.CENTER);
        layout.setPadding(dp2px(context, 16), dp2px(context, 12), dp2px(context, 16), dp2px(context, 12));
        layout.setBackground(drawable);
        ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(dp2px(context, 48), dp2px(context, 48)));
        int imageRes = ICON_FINISH;
        switch (status) {
            case 1:
                imageRes = ICON_FINISH;
                break;
            case 2:
                imageRes = ICON_ERROR;
                break;
            case 3:
                imageRes = ICON_WARNING;
                break;
        }
        imageView.setImageResource(imageRes);
        layout.addView(imageView);
        FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        layoutParams1.topMargin = dp2px(context, 8);
        TextView textView = new TextView(context);
        textView.setLayoutParams(layoutParams1);
        textView.setTextSize(15);
        textView.setMaxEms(12);
        textView.setText(message);
        textView.setLineSpacing(dp2px(context, 4), 1f);
        textView.setTextColor(Color.WHITE);

        layout.addView(textView);

        return layout;
    }

    private static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
